package com.zhongketech.sunflower.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.Thumbnails;
import android.support.v4.view.ViewPager;
import android.text.ClipboardManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.adapter.ExpressionAdapter;
import com.zhongketech.sunflower.adapter.ExpressionPagerAdapter;
import com.zhongketech.sunflower.adapter.FcsContact;
import com.zhongketech.sunflower.adapter.FcsContactAdapter;
import com.zhongketech.sunflower.adapter.MessageAdapter;
import com.zhongketech.sunflower.adapter.VoicePlayClickListener;
import com.zhongketech.sunflower.app.AppManager;
import com.zhongketech.sunflower.transport.Action;
import com.zhongketech.sunflower.transport.LogUtil;
import com.zhongketech.sunflower.transport.im.ZKSocket;
import com.zhongketech.sunflower.transport.model.Authorizations;
import com.zhongketech.sunflower.transport.model.DaoSession;
import com.zhongketech.sunflower.transport.model.MyRequest;
import com.zhongketech.sunflower.transport.model.Parents;
import com.zhongketech.sunflower.transport.model.Request;
import com.zhongketech.sunflower.transport.model.Supervision;
import com.zhongketech.sunflower.transport.model.User;
import com.zhongketech.sunflower.utils.CommonUtils;
import com.zhongketech.sunflower.utils.ImageUtil;
import com.zhongketech.sunflower.utils.SmileUtils;




/**
 * 绑定界面
 * 
 */
@SuppressLint("HandlerLeak")
public class BindActivity extends BaseActivity   {


	private static final String TAG = "BindActivity";

	private ListView listView ;
	private Context context;

	private List<FcsContact> myData = new ArrayList<FcsContact>();
	private Handler handler ;
	private FcsContactAdapter adapter;
	private DaoSession ds;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fcs_contacts_activity);
		context = this;
		initData();
		initView();

		handler = new Handler() {
			public void handleMessage(Message msg) {
				adapter.notifyDataSetChanged();
			}
		};
	}


	@Override  
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(KeyEvent.KEYCODE_BACK == keyCode)
		{
			back();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	private void initData()
	{
		ds = MyApplication.getInstance().getSupervisonsDaoSession();

		new Thread(new Runnable() {
			public void run() {
				Looper.prepare();

				//get from db
				List<Request> rqDbList = ds.getRequestDao().loadAll();
				for(int j = 0;null != rqDbList && j < rqDbList.size();j ++)
				{
					Request rqDb = rqDbList.get(j);
					if(Constant.RQ_PARENT.equals(rqDb.getType()) || Constant.RQ_CLASS.equals(rqDb.getType()))
					{

						FcsContact ad = new FcsContact();
						ad.imageViewId = R.drawable.default_contacts_head;

						User user = new Action().user(rqDb.getFrom());
						if(null != user)
						{
							ad.itemText = user.getName();
							ad.imageUrl = user.getAvatar_url();
						}
						else
						{
							ad.itemText = "该用户不存在";
						}
						ad.id = rqDb.getId();
						ad.textview2 = rqDb.getText();
						ad.statusText = "已接受";
						myData.add(ad);

					}
				}

				MyRequest requests = new Action().requests();
				if(null != requests)
				{	
					if(null == requests.getRequests() || requests.getRequests().size() == 0)
					{
						LogUtil.i(TAG,"没有相关的请求信息");
					}
					else
					{
						for(int i = 0;i < requests.getRequests().size();i ++)
						{
							Request rq = requests.getRequests().get(i);
							if(Constant.RQ_PARENT.equals(rq.getType()) || Constant.RQ_CLASS.equals(rq.getType()))
							{
								if(Constant.REQUESTS_STATE_WAIT.equals(rq.getState()))
								{
									FcsContact ad = new FcsContact();
									ad.imageViewId = R.drawable.default_contacts_head;

									User user = new Action().user(rq.getFrom());
									if(null != user)
									{
										ad.itemText = user.getName();
										ad.imageUrl = user.getAvatar_url();
									}
									else
									{
										ad.itemText = "该用户不存在";
									}
									ad.id = rq.getId();
									ad.textview2 = rq.getText();
									boolean isSaveDb = false;
									for(int k = 0; null != myData && k < myData.size();k ++)
									{
										if(rq.getId().equals(myData.get(k).id))
										{
											myData.get(k).statusText = "接受";
											isSaveDb = true;
										}
									}
									ad.statusText = "接受";
									if(!isSaveDb)
									{
										ds.getRequestDao().insert(rq);
										myData.add(ad);
									}

								}


							}
						}
					}
				}
				else
				{
					MyToast.getInstance().show("没有请求信息");
				}

				Collections.sort(myData,new SortComparator());
				final Message msg = new Message();
				handler.sendMessage(msg);
			}
		}).start();
	}

	public class SortComparator implements Comparator
	{

		@Override
		public int compare(Object arg0, Object arg1) {
			// TODO Auto-generated method stub

			FcsContact fc0 = (FcsContact) arg0; FcsContact fc1 = (FcsContact) arg1;

			return fc0.statusText.length() - fc1.statusText.length();
		}

	}

	private void initView()
	{
		((TextView) findViewById(R.id.title)).setText("新的绑定");
		listView = (ListView) findViewById(R.id.listView);
		adapter = new FcsContactAdapter(myData,Constant.FCS_ADAPTER_TYPE_BIND);
		listView.setAdapter(adapter);
	}


	private void back()
	{
		finish();
	}

}
